package com.example.queryexpressdelivery_01.fragment;

import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.queryexpressdelivery_01.common.HttpGetData;
import com.example.queryexpressdelivery_01.adapter.IndexRvAdapter;
import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.entity.CompanyFragment_02;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BillQueryCompanyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BillQueryCompanyFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private String sTitle;
    private TextView text01;
    private Button button2;
    private EditText text02;
    private RecyclerView rv002;
    private Button button03;

    public BillQueryCompanyFragment() {
        // Required empty public constructor
    }

    public BillQueryCompanyFragment(String sTitle) {
        this.sTitle = sTitle;
        Bundle d1 = new Bundle();
        d1.putString("param1", sTitle);
        this.setArguments(d1);
    }

    public String getTitle() {
        return sTitle;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BillQueryCompanyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BillQueryCompanyFragment newInstance(String param1, String param2) {
        BillQueryCompanyFragment fragment = new BillQueryCompanyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    private void clearRecyclerView() {
        IndexRvAdapter adapter = (IndexRvAdapter) rv002.getAdapter();
        if (adapter != null) {
            adapter.setExpressList(new ArrayList<>());
            adapter.notifyDataSetChanged();
        }
    }
    public void onStart() {
        super.onStart();
        button03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                text02.setText("");
                clearRecyclerView();
            }
        });
        //01
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String trackingNumber = text02.getText().toString().trim();
                if (trackingNumber.isEmpty()) {
                    text01.setText("快递单号为空");
                } else {
                    rv002.setLayoutManager(new LinearLayoutManager(getContext()));
                    List<Map<String, String>> expressInfo = new ArrayList<>();
                    IndexRvAdapter adapter = new IndexRvAdapter();
                    adapter.setExpressList(expressInfo);
                    rv002.setAdapter(adapter);

                    HttpGetData httpGetData = new HttpGetData(trackingNumber, null, null);
                    String jsonResponse = httpGetData.QueryExpressNumberCompany();
                    Gson gson = new Gson();
                    CompanyFragment_02 response = gson.fromJson(jsonResponse, CompanyFragment_02.class);


                    if (response.getStatus().equals("0")) {  // 如果状态为0，说明单号数据正常
                        text01.setText("");

                        Map<String, String> status = new ArrayMap<>();
                        status.put("查询状态：", "正常");

                        Map<String, String> number = new ArrayMap<>();
                        number.put("快递单号：", response.getNumber());


                        Set<String> addedCompanies = new HashSet<>();//2

                        if (!response.getList().isEmpty()) {
                            for (int i = 0; i < response.getList().size(); i++) {
                                CompanyFragment_02.Company company = response.getList().get(i);


                                String companyName = company.getName();//2
                                if (!addedCompanies.contains(companyName)) {
                                    Map<String, String> name = new ArrayMap<>();
                                    name.put("快递公司：", companyName);
                                    expressInfo.add(name);
                                    addedCompanies.add(companyName);
                                }


                            }
                        }

                        expressInfo.add(status);
                        expressInfo.add(number);

                        adapter.setExpressList(expressInfo);
                    } else {
                        text01.setText("请输入有效的快递单号");
                    }
                }

            }

        });
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_bill_query_company, container, false);

        initView(view);
        return view;
    }

    private void initView(View view) {
        button2 = (Button) view.findViewById(R.id.button2);
        text02 = (EditText) view.findViewById(R.id.text02);
        text01 = (TextView) view.findViewById(R.id.text01);
        rv002 = (RecyclerView) view.findViewById(R.id.rv002);
        button03 = (Button) view.findViewById(R.id.button03);
    }
}